package bioinfo.stats;

import bioinfo.*;
import javax.swing.*;
import java.awt.*;
import java.util.Properties;
import java.util.Hashtable;
import java.util.Enumeration;

import com.approximatrix.charting.swing.*;
import com.approximatrix.charting.model.*;
import com.approximatrix.charting.render.*;
import com.approximatrix.charting.*;

public class Second extends Stats {
    private Hashtable<String, Integer> hash;
    private ChartPanel chart;
    private JPanel jp_charts;

    public Second() {
        hash = new Hashtable<String, Integer>();

        for(ARN arn_tmp : Main.arns) {
            String anticodon = arn_tmp.getAnticodon();

            Integer tmp = (hash.get(anticodon) == null) ? 0 : hash.get(anticodon);
            hash.put(anticodon, tmp+1);
        }

        Enumeration<String> keys = hash.keys();
        Enumeration<Integer> elem = hash.elements();

        Double data[][] = new Double[hash.size()][1];
        String[] names = new String[hash.size()];
        String[] column_id = {"Percent of each anticodon"};

        int i = 0;
        while(keys.hasMoreElements() && elem.hasMoreElements()) {
            Integer tmp = elem.nextElement();
            String tmp_name = keys.nextElement();

            double percent = new Double(tmp)*100/new Double(Main.arns.size());
            names[i] = tmp_name;
            data[i][0] = percent;

            i++;
        }
        ChartDataModel model = new ObjectChartDataModel(data, column_id, names);

        PieChartRenderer renderer = new PieChartRenderer(model);

        CoordSystem coord = new CoordSystem(model);
        coord.setPaintAxes(false);

        chart = new ChartPanel(model,"Pourcentage d'apparition des anticodons");
        chart.addChartRenderer(renderer, 0);
        chart.setCoordSystem(coord);

        jp_charts = new JPanel(new BorderLayout());
        //jp_charts.add(new JLabel("test"), BorderLayout.NORTH);
        jp_charts.add(chart);
    }

    public JPanel getPanel() {
        return jp_charts;
    }
}

